/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function (l1, l2) {
    let carry = 0, result;
    while (l1 || l2 || carry) {
        var v1 = 0,
            v2 = 0;
        if (l1) {
            v1 = l1.val;
            l1 = l1.next;
        }
        if (l2) {
            v2 = l2.val;
            l2 = l2.next;
        }
        let sum = v1 + v2 + carry;
        carry = Math.floor(sum / 10);
        if (!result) {
            result = new ListNode(sum % 10);
            resultCopy=result;
        } else {
            resultCopy.next = new ListNode(sum % 10)
            resultCopy = resultCopy.next;
        }
    }
    return result;
};